#include <cstdio>
#include <vector>
using namespace std;

const char* str[] = {"front", "left", "top", "back", "right", "bottom"};
int N, H, X;
const int FACE = 6, HALF_FACE = 3;
const int MAX_COLORS = 100;
vector< int > order;
vector< int > face;

struct Cube {
    Cube() {}
    int color[FACE];    //  origin: front, back, left, right, top and bottom
                        //  stored: front, left, top, back, right, bottom
                        //  indexs:   0      1    2    3      4      5
};

vector< Cube > vec_c;

void input() {
    vec_c.clear();
    for(int i = 1; i <= N; ++i) {
        Cube new_cube;
        int color;
        for(int j = 0; j < 6; ++j) {
            scanf("%d", &color);
            switch(j) {
                case 0:
                    new_cube.color[0] = color;
                break;
                case 1:
                    new_cube.color[3] = color;
                break;
                case 2:
                    new_cube.color[1] = color;
                break;
                case 3:
                    new_cube.color[4] = color;
                break;
                case 4:
                    new_cube.color[2] = color;
                break;
                case 5:
                    new_cube.color[5] = color;
                break;
            }
        }
        vec_c.push_back(new_cube);
    }
}

int cal() {
    vector< int > color_length(MAX_COLORS + 1, 0);
    vector< int > seq;
    vector< vector< int > > seq2d(MAX_COLORS + 1, seq);
    vector< vector< int > > face2d(MAX_COLORS + 1, seq);
    for(int i = 0; i < N; ++i) {
        Cube old_cude = vec_c[i];
        vector< int > expand(MAX_COLORS + 1, 0);
        vector< vector< int > > tmp_seq2d(MAX_COLORS + 1, seq);
        vector< vector< int > > tmp_face2d(MAX_COLORS + 1, seq);
        for(int j = 0; j < FACE; ++j) {
            int top_color = old_cude.color[j];
            int bottom_color = old_cude.color[(j + HALF_FACE) % FACE];
            if(color_length[top_color] + 1 > color_length[bottom_color] &&
               color_length[top_color] + 1 - color_length[bottom_color] > expand[bottom_color]) {
                tmp_seq2d[bottom_color] = seq2d[top_color];
                tmp_seq2d[bottom_color].push_back(i);
                tmp_face2d[bottom_color] = face2d[top_color];
                tmp_face2d[bottom_color].push_back(j);
                expand[bottom_color] = color_length[top_color] + 1 - color_length[bottom_color];
            }
        }
        for(int i = 0; i <= MAX_COLORS; ++i) {
            color_length[i] += expand[i];
            if(expand[i] > 0) {
                seq2d[i] = tmp_seq2d[i];
                face2d[i] = tmp_face2d[i];
            }
        }
    }
    H = 0;
    X = -1;
    for(int i = MAX_COLORS; i >= 0; --i) {
        if(H < color_length[i]) {
            H = color_length[i];
            X = i;
        }
    }
    order = seq2d[X];
    face = face2d[X];
    return H;
}

void output(int cases) {
    if(cases > 1) {
        puts("");
    }
    printf("Case #%d\n%d\n", cases, cal());
    for(int i = 0, j = order.size(); i < j; ++i) {
        printf("%d %s\n", order[i] + 1, str[ face[i] ]);
    }
}

int main() {
    for(int cases = 1; ~scanf("%d", &N) && N > 0; ++cases) {
        input();
        output(cases);
    }
    return 0;
}
